//
//  GAAuthenticationViewController.m
//  HYApp
//
//  Created by Jianbo Cai on 2020/8/21.
//  Copyright © 2020 Unidax. All rights reserved.
//

#import "GAAuthenticationViewController.h"
#import "ChangeVerifyView.h"

@interface GAAuthenticationViewController ()
AWPString(googleKey)
@end

@implementation GAAuthenticationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.navigationItem.title = JFMainLocalizedString(@"myProfile.Safety");
    WEAKSELF
    [HYNetHelper users_2fa_toggle:YES verifyType:@"1" token:@"" callback:^(JFResultVo *resultVo) {
        if (resultVo.errorNo == 0) {//成功就是开启
            NSString * qr = resultVo.results[@"qr"];
            NSString * base64qr = [[qr componentsSeparatedByString:@","] lastObject];
            NSData *imageData = [[NSData alloc] initWithBase64EncodedString:base64qr options:NSDataBase64DecodingIgnoreUnknownCharacters];
            weakSelf.qrcode.image = [UIImage imageWithData:imageData];
            weakSelf.qrStr.text = resultVo.results[@"secret"];
            weakSelf.googleKey = resultVo.results[@"key"];
            [HYNetHelper setAuthToken:resultVo.noShowErrorInfo];
        }
    }];

}



- (IBAction)goNext:(id)sender {
    
   ChangeVerifyView *view = [[[NSBundle mainBundle] loadNibNamed:@"ChangeVerifyView" owner:nil options:nil] firstObject];
   view.frame = CGRectMake(0, 0, CUSTOMVIEW_WIDTH, 110);
   FZAlertController *vc = [FZAlertController alertControllerWithTitleImage:[UIImage imageNamed:@"Captcha_ico_b"] title:nil customView:view message:nil];
   vc.showCloseBtn = YES;
   WEAKSELF
   FZAlertAction *action = [FZAlertAction actionWithTitle:JFMainLocalizedString(@"confirm") style:UIAlertActionStyleDefault handler:^(FZAlertAction *action) {
       NSString * token  = view.codeText.text;
       [HYNetHelper users_2fa_verify:weakSelf.googleKey tfaToken:token callback:^(JFResultVo *resultVo) {
           if (resultVo.errorNo == 0 && [resultVo.results boolForKey:@"result"]) {
               [HYNetHelper users_2fa:^(JFResultVo *resultVo) {
                   if (resultVo.errorNo == 0) {
                       [SLRemindView showMessage:JFMainLocalizedString(@"myProfile.success") toView:weakSelf.view];
                   }
               }];
           }else{
               [SLRemindView showMessage:JFMainLocalizedString(@"transactions.failed") toView:weakSelf.view];
           }
       }];
   }];
   [vc addAction:action];
   [self presentViewController:vc animated:YES completion:nil];
    
}

- (IBAction)copyStr:(id)sender {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = self.qrStr.text;
    
}


@end
